#include <curl/curl.h>
#include <cjson/cJSON.h>

//回调函数
size_t writrCallback(void *contents,size_t size,size_t nmemb,void *userp){
    size_t realsize=size*nmemb;
    char *result=(char *)contents;

    cJSON* root=cJSON_Parse(result);
    if (root !=NULL)
    {
        cJSON* successJson=cJSON_GetObjectItemCaseSensitive(root,"success");
        cJSON* codeJson=cJSON_GetObjectItemCaseSensitive(root,"code");
        cJSON* messageJson=cJSON_GetObjectItemCaseSensitive(root,"message");
        printf("sucess=%f,code=%d,message=%s\n",successJson->valuedouble,codeJson->valueint,messageJson->valuestring);

    }

    printf("%s\n",result);
    return realsize;
    
}

int main(int argc, char const *argv[])
{
    CURL *curl=curl_easy_init();
    if (curl)
    {
        //请求配置，设置路径，设置回调函数
        curl_easy_setopt(curl,CURLOPT_URL,"http://47.109.65.208/config.json");
        curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,writrCallback);

        //发起请求
        CURLcode code=curl_easy_perform(curl);
        if (code != CURLE_OK)
        {
            fprintf(stderr,"curl_easy_perform() failed: %s\n",curl_easy_strerror(code));
        }
        
        //清理curl对象
        curl_easy_cleanup(curl);
    }
    
    return 0;
}
